package com.dxy.lc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dxy.lc.model.bean.UpdPwdDto;
import com.dxy.lc.model.entity.User;
import com.dxy.lc.model.bean.TreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.dxy.lc.base.Resp;
import java.util.List;
import com.dxy.lc.service.UserService;

/**
* <p>
* 用户表 controller
* </p>
*
* @author dxy
* @since 2024-09-23
*/
@Slf4j
@RestController
@RequestMapping("/lc/user")
public class UserController {

    @Autowired
    private UserService service;

    @PostMapping("/add")
    public Resp add(@RequestBody User entity) {
        service.add(entity);
        return Resp.bizSuccess();
    }

    @PostMapping("/updById")
    public Resp updById(@RequestBody User entity) {
        service.updById(entity);
        return Resp.bizSuccess();
    }

    @PostMapping("/delByIds")
    public Resp delByIds(@RequestBody List<Integer> ids) {
        service.delByIds(ids);
        return Resp.bizSuccess();
    }

    @GetMapping("/getDetail")
    public Resp<User> getDetail(Integer id) {
        return Resp.bizSuccess(service.getDetail(id));
    }

    @PostMapping("/updPwd")
    public Resp updPwd(@RequestBody UpdPwdDto dto) {
        service.updPwd(dto);
        return Resp.bizSuccess();
    }

    @GetMapping("/getLoginUserInfo")
    public Resp<User> getLoginUserInfo() {
        return Resp.bizSuccess(service.getLoginUserInfo());
    }

    @GetMapping("/getContactBook")
    public Resp<List<TreeVo>> getContactBook(String kw){
        return Resp.bizSuccess(service.getContactBook(kw));
    }

    @GetMapping("count")
    public Resp<Integer> count(String deptId){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .likeRight(StrUtil.isNotEmpty(deptId), User::getDeptId, deptId);
        return Resp.bizSuccess((int)service.count(wrapper));
    }

}